package com.splic.web.controller.product;

import java.util.List;
import com.splic.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.splic.common.annotation.Log;
import com.splic.common.enums.BusinessType;
import com.splic.product.domain.City;
import com.splic.product.service.CityService;
import com.splic.common.core.controller.BaseController;
import com.splic.common.core.domain.AjaxResult;
import com.splic.common.utils.poi.ExcelUtil;
import com.splic.common.core.page.TableDataInfo;

/**
 * 城市设置Controller
 * 
 * @author singe
 * @date 2022-03-08
 */
@Controller
@RequestMapping("/product/city")
public class CityController extends BaseController
{
    private String prefix = "product/city";

    @Autowired
    private CityService cityServiceImpl;

    @RequiresPermissions("product:city:view")
    @GetMapping()
    public String city()
    {
        return prefix + "/list";
    }

    /**
     * 查询城市设置列表
     */
    @RequiresPermissions("product:city:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(City city)
    {
        startPage();
        List<City> list = cityServiceImpl.selectCityList(city);
        return getDataTable(list);
    }

    /**
     * 新增城市设置
     */
    @GetMapping(value = { "/form/{cityId}", "/form" })
    public String form(@PathVariable(value = "cityId", required = false) Long cityId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(cityId)){
            mmap.put("city", cityServiceImpl.selectCityByCityId(cityId));
        }else{
            mmap.put("city", new City());
        }
        return prefix + "/form";
    }


    /**
     * 新增保存城市设置
     */
    @RequiresPermissions("product:city:edit")
    @Log(title = "城市设置", businessType = BusinessType.INSERT)
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public AjaxResult saveOrUpdate(City city)
    {
        return toAjax(cityServiceImpl.saveOrUpdate(city));
    }

    /**
     * 删除
     */
    @RequiresPermissions("product:city:remove")
    @Log(title = "城市设置", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long cityId)
    {
        return toAjax(cityServiceImpl.deleteById(cityId));
    }


    /**
     * 导出城市设置列表
     */
    @RequiresPermissions("product:city:export")
    @Log(title = "城市设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(City city)
    {
        List<City> list = cityServiceImpl.selectCityList(city);
        ExcelUtil<City> util = new ExcelUtil<City>(City.class);
        return util.exportExcel(list, "城市设置数据");
    }

}
